﻿using System;
using System.Collections.Generic;
using JetBrains.Annotations;
using JetBrains.DocumentModel;
using JetBrains.ReSharper.Daemon;
using JetBrains.ReSharper.Daemon.Impl;
using JetBrains.ReSharper.Psi;
using JetBrains.ReSharper.Psi.CSharp;
using JetBrains.ReSharper.Psi.CSharp.Tree;
using MugenMvvmToolkit.ReSharper.Highlighting;

[assembly:
    RegisterConfigurableSeverity(InvalidValidateMethodCallHighlighting.Id, null, HighlightingGroupIds.CompilerWarnings,
        "Invalid property name in Validate method call", "Invalid property name in Validate method call", Severity.WARNING, false)]

namespace MugenMvvmToolkit.ReSharper.Highlighting
{
    [ConfigurableSeverityHighlighting(Id, CSharpLanguage.Name, ShowToolTipInStatusBar = true,
       OverlapResolve = OverlapResolveKind.WARNING, AttributeId = HighlightingAttributeIds.WARNING_ATTRIBUTE)]
    public class InvalidValidateMethodCallHighlighting : IHighlightingWithRange
    {
        #region Fields

        public const string Id = "InvalidValidateMethodCallHighlighting";

        #endregion

        #region Constructors

        /// <summary>
        ///     Initializes a new instance of the <see cref="InvalidModelPropertyAttributeHighlighting" /> class.
        /// </summary>
        public InvalidValidateMethodCallHighlighting([NotNull] IClass modelClass,
            [NotNull] ICSharpLiteralExpression literalExpression, [NotNull] IEnumerable<string> properties)
        {
            if (literalExpression == null) throw new ArgumentNullException("literalExpression");
            if (properties == null) throw new ArgumentNullException("properties");
            if (modelClass == null) throw new ArgumentNullException("modelClass");
            ModelClass = modelClass;
            Literal = literalExpression;
            Properties = properties;
        }

        #endregion

        #region Implementation of IHighlighting

        /// <summary>
        ///     Returns true if data (PSI, text ranges) associated with highlighting is valid
        /// </summary>
        public bool IsValid()
        {
            return Literal.IsValid();
        }

        /// <summary>
        ///     Message for this highlighting to show in tooltip and in status bar (if
        ///     <see cref="P:JetBrains.ReSharper.Daemon.HighlightingAttributeBase.ShowToolTipInStatusBar" /> is <c>true</c>)
        ///     To override the default mechanism of tooltip, mark the implementation class with
        ///     <see cref="T:JetBrains.ReSharper.Daemon.DaemonTooltipProviderAttribute" /> attribute, and then this property will
        ///     not be called
        /// </summary>
        public string ToolTip
        {
            get
            {
                return string.Format(UiResources.InvalidValidateMethodCallFormat2, ModelClass.ShortName,
                    Literal.ConstantValue.Value);
            }
        }

        /// <summary>
        ///     Message for this highlighting to show in tooltip and in status bar (if
        ///     <see cref="P:JetBrains.ReSharper.Daemon.HighlightingAttributeBase.ShowToolTipInStatusBar" /> is <c>true</c>)
        /// </summary>
        public string ErrorStripeToolTip
        {
            get { return ToolTip; }
        }

        /// <summary>
        ///     Specifies the offset from the Range.StartOffset to set the cursor to when navigating
        ///     to this highlighting. Usually returns <c>0</c>
        /// </summary>
        public int NavigationOffsetPatch
        {
            get { return 0; }
        }

        public DocumentRange CalculateRange()
        {
            return Literal.GetLiteralHighlightingRange();
        }

        #endregion

        #region Properties

        public IClass ModelClass { get; private set; }

        public ICSharpLiteralExpression Literal { get; private set; }

        public IEnumerable<string> Properties { get; private set; }

        #endregion
    }
}